
 function formatarNumero(pNumero, pQtdeDigFrac, pSimbFrac, pQtdeDigAgrup, pSimbAgrup) {

      numAux = pNumero.toFixed(pQtdeDigFrac).replace(/\./g, pSimbFrac);
      numParts = numAux.split(pSimbFrac);  qtdeDigInt = numParts[0].length;
      qtdeSimbAgrup = qtdeDigInt / pQtdeDigAgrup;
      if (qtdeDigInt % pQtdeDigAgrup == 0) qtdeSimbAgrup--;

      for (i = 1; i <= qtdeSimbAgrup; i++) {
           numAux = numParts[0].substring(0, qtdeDigInt - pQtdeDigAgrup * i);
           numParts[0] = numAux + pSimbAgrup + numParts[0].substring(qtdeDigInt - pQtdeDigAgrup * i);
      }
      return numParts[0] + pSimbFrac + numParts[1];
 }

 function mascararNumeroRL(pCampo, pEvento, pQtdeDigInt, pQtdeDigFrac, pSimbAgrup, pSimbFrac, pQtdeDigAgrup) {

      if (pEvento.keyCode == 9) return;
      
      valorTmp = pCampo.value.replace(/\D/g, '');
      tamTotal = pQtdeDigInt + pQtdeDigFrac;
  
      tamValor = valorTmp.length;
      if (tamValor > tamTotal) valorTmp = valorTmp.substring(0, tamTotal);
      tamValor = valorTmp.length;
  
      if (tamValor > pQtdeDigInt) {
          valorTmp = valorTmp.substring(0, pQtdeDigInt) + pSimbFrac + valorTmp.substring(pQtdeDigInt);
          tamValor = pQtdeDigInt;
      }
      posInsDig = tamValor % pQtdeDigAgrup;
      qtdeAgrup = parseInt(tamValor / pQtdeDigAgrup);
      if (posInsDig == 0) qtdeAgrup--;
      posInsDig = tamValor - pQtdeDigAgrup;

      for (i = 0; i < qtdeAgrup; i++) {
          valorTmp = valorTmp.substring(0, posInsDig) + pSimbAgrup + valorTmp.substring(posInsDig);
          posInsDig = posInsDig - pQtdeDigAgrup;
      }
      pCampo.value = valorTmp;
 }
  
 function mascararNumeroLR(pCampo, pEvento, pQtdeDigInt, pQtdeDigFrac, pSimbAgrup, pSimbFrac, pQtdeDigAgrup) {

      if (pEvento.keyCode == 9) return;

      valorTmp = pCampo.value.replace(/\D/g, '');
      tamTotal = pQtdeDigInt + pQtdeDigFrac;
  
      tamValor = valorTmp.length;
      if (tamValor > tamTotal) valorTmp = valorTmp.substring(tamValor - tamTotal);
      tamValor = valorTmp.length;
  
      posInsDig = tamValor - pQtdeDigFrac;
      qtdeAgrup = parseInt(posInsDig / pQtdeDigAgrup);
      if (posInsDig % pQtdeDigAgrup == 0) qtdeAgrup--;
  
      if (pQtdeDigFrac > 0 && tamValor > pQtdeDigFrac) {
          valorTmp = valorTmp.substring(0, posInsDig) + pSimbFrac + valorTmp.substring(posInsDig);
      }
      for (i = 0; i < qtdeAgrup; i++) {
          posInsDig = posInsDig - pQtdeDigAgrup;
          valorTmp = valorTmp.substring(0, posInsDig) + pSimbAgrup + valorTmp.substring(posInsDig);
      }
      pCampo.value = valorTmp;
 }
  
 function exibirEsconder(pElemId) {

	 var lElemVisibility = document.getElementById(pElemId).style.visibility;
	 document.getElementById(pElemId).style.visibility = (lElemVisibility == "hidden") ? "visible" : "hidden";
 }

 function mostrarOcultar(pElemId) {

	var lElemDisplay = document.getElementById(pElemId).style.display;
	document.getElementById(pElemId).style.display = (lElemDisplay == "none") ? "block" : "none";
 }

 function abrirPopup(pLocation, pWidth, pHeight, pNomePopup) {

        var x, y;
        var features  = "width=" + pWidth + ",height=" + pHeight;
        // Netscape
        if (window.screenX) {
            x = window.screenX + (window.outerWidth - pWidth) / 2;
            y = window.screenY + (window.outerHeight - pHeight) / 2;
            features += ",screenX=" + x + ",screenY=" + y;
        }
        // IE
        else {
            x = (window.screen.availWidth - pWidth) / 2;
            y = (window.screen.availHeight - pHeight) / 2;
            features += ",left=" + x + ",top=" + y;
        }
        features += ",toolbar=no,status=no, scrollbars=yes";

        // retorna a refer?ncia ? janela, para poder fech?-la mais tarde
        return window.open(pLocation, pNomePopup, features);
 }

 function trim(pStr) {
    return pStr.replace(/^\s+/,'').replace(/\s+$/,'');
 }

 function validarNumero(pNumero) {

       var lER = /(\.|,)/g;
       pNumero = pNumero.replace(lER,"");

       if (isNaN(pNumero)) return false;

       return true;
 }

 function validarData(pData) {

       var lERData = new RegExp("^(?:(?:31(\\/|-|\\.)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.)"+
	        "(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.)0?2\\3(?:(?"+
	        ":(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])"+
	        "00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$");

	return lERData.test(pData);
 }

 function validarCNPJ(pCNPJ) {

	var lER = /(\.|-|\/)/g;

	pCNPJ = pCNPJ.replace(lER,"");

	var dig1 = 0;
	var dig2 = 0;
	var Mult1 = '543298765432';
	var Mult2 = '6543298765432';

	for(x = 0; x <= 11; x++) {
		dig1 = dig1 +(parseInt(pCNPJ.slice(x,x+1)) * parseInt(Mult1.slice(x, x+1)) );
	}

	for(x = 0; x <= 12; x++) {
		dig2 = dig2 + (parseInt(pCNPJ.slice(x, x+1)) * parseInt(Mult2.slice(x, x+1)) );
	}

	dig1 = (dig1 * 10)%11;
	dig2 = (dig2 * 10)%11;

	if (dig1 == 10) {dig1 = 0;}
	if (dig2 == 10) {dig2 = 0;}

	if (dig1 != parseInt(pCNPJ.slice(12, 13))) {
		return false;

	} else {
		if (dig2 != parseInt(pCNPJ.slice(13, 14))) {
			return false;
		} else {
			return true;
		}
	}
 }

 function validarCPF(pCPF) {

	var lER = /(\.|-)/g;

	pCPF = pCPF.replace(lER,"");

	x = 0;
	soma = 0;
	dig1 = 0;
	dig2 = 0;
	texto = "";
	numcpf1 = "";
	len = pCPF.length; x = len -1;

	for (var i = 0; i <= len - 3; i++) {
		y = pCPF.substring(i,i+1);
		soma = soma + ( y * x);
		x = x - 1;
		texto = texto + y;
	}
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1 = 0;
	if (dig1 == 11) dig1 = 0;
	numcpf1 = pCPF.substring(0,len - 2) + dig1 ;
	x = 11; soma = 0;
	for (var i=0; i <= len - 2; i++) {
		soma = soma + (numcpf1.substring(i,i+1) * x);
		x = x - 1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 == 10) dig2 = 0;
	if (dig2 == 11) dig2 = 0;

	var digCalculado = parseInt(dig1 + "" + dig2);
	var digOriginal = parseInt(pCPF.substring(len-2, len));

	if ( digCalculado == digOriginal ) {

		return true;
	}

	return false;
 }

 function mudarBGColor(pElem, pNovaCor) {
 	
 	pElem.style.backgroundColor = pNovaCor;
 }
